package test.匹配pmid图片相关;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import lombok.SneakyThrows;
import utils.SmallTool;
import utils.StringUtil;

import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022-4-18 9:35
 */
public class GetBossPmidAndPic1 {

    public static String insert = "pmiddoidown_gcr_local";


    public static boolean isCheck = true;

    @SneakyThrows
    public static void main(String[] args) {
        // 获取老板的pmid 和figure 插入表

        // -Xmx68660m -Xms22560m
        Db use = Db.use(DSFactory.get("local_boss"));
        Db useLocalPm = Db.use();

        String dbName = CommonTable.pmiddoiFromBossHtml;

        int startPmid = 0;
        String allDate = "select max(pmid) as pmid  from " + dbName;
        Entity query = use.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));

        Integer count = query.getInt("pmid");
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);

        SmallTool.printMemory();

        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");

        for (int i = 1; i <= pageSize; i++) {
            SmallTool.printMess("当前页码+++++" + i);
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            if (end <= startPmid) {
                SmallTool.printMess("因为小于" + startPmid + " 略过了");
                continue;
            }
            String paramSql = "select pmid,figure from " + dbName + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = use.query(paramSql);
            querys.parallelStream().forEach(entity -> {
                Integer pmid = entity.getInt("pmid");
                String figure = entity.getStr("figure");
                if (StringUtil.isNotNull(figure) && !figure.equals("[]")) {
                    try {
                        insertData(pmid, figure, useLocalPm);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }


            });
        }


    }

    @SneakyThrows
    public static void insertData(Integer pmid, String doi, Db use) {
        if (isCheck) {
            String sql = "select * from " + insert + " where pmid = ?";
            Entity entity = use.queryOne(sql, pmid);
            if (entity == null) {
                use.insert(new Entity().setTableName(insert)
                        .set("pmid", pmid)
                        .set("figure", doi)
                        .set("status", 0)
                        .set("isok", 0)
                );
            }

        } else {
            use.insert(new Entity().setTableName(insert)
                    .set("pmid", pmid)
                    .set("figure", doi)
                    .set("status", 0)
                    .set("isok", 0)
            );
        }


    }
}
